import Vue from 'vue'
import Vuex from 'vuex'
import SDK from '@/utils/IM/sdk/NIM_Web_SDK_weixin_v7.2.0.js'
import config from '@/utils/IM/neteaseConfig.js'

Vue.use(Vuex)

export default new Vuex.Store({
	state: {
		userInfo: uni.getStorageSync('TZ_userInfo') ? uni.getStorageSync('TZ_userInfo') :
		null, // type 1 普通用户  2  运用中心  3 总部
		share_baseurl: '',
		token: uni.getStorageSync('TZ_token') ? uni.getStorageSync('TZ_token') : null,
		clientRect: {},
		statusBarHeight: 0,
		uploadBack: [],
		invitation_code: uni.getStorageSync('TZ_invitation_code') ? uni.getStorageSync('TZ_invitation_code') :
			null,
		newUser: 2, // 1 是 2 否
		currentAreaSpare: uni.getStorageSync('TZ_currentAreaSpare') ? uni.getStorageSync(
			'TZ_currentAreaSpare') : null, // 由于改变需求20211206
		currentArea: uni.getStorageSync('TZ_currentArea') ? uni.getStorageSync('TZ_currentArea') :
		null, // 当前定位的区域
		addressSelect: uni.getStorageSync('TZ_addressSelect') ? uni.getStorageSync('TZ_addressSelect') :
		'', // 区域搜索已选地址
		currentAddressList: uni.getStorageSync('TZ_currentAddressList') ? uni.getStorageSync(
			'TZ_currentAddressList') : '', // 城市列表历史
		// findDesignList:null,
		// findCraftsmanList:null,
		// buildingMaterialsCityList:null,
		// myRecommendList:null,
		// HomeFindInspiration:null,
		// HomeBannerList:null,
		// HomeTellAboutList:null,
		cityData: null,
		isOpenSetting: 0,
		isOpenSettingshow: 0,
		choiceHouse: null,
		defaultHouse: uni.getStorageSync('TZ_defaultHouse') ? uni.getStorageSync('TZ_defaultHouse') : null,
		duanType: uni.getStorageSync('TZ_duanType') ? uni.getStorageSync('TZ_duanType') : 1, // 1 app 2 微信,
		chat_info:[],
		chat_to:null,
		chat_state:false,
		chat_list:uni.getStorageSync('TZ_chat_list') ? uni.getStorageSync('TZ_chat_list'):[],
		chat_unread:0,
		chat_commodityInfo:null,
		chat_offer_current:null,
		isIOS:false,
		
		// 蓝色系主题
		yellow: {
		      '--text-color': '#FFCB41',
		      '--text-color-reverse': '#FFCB41',
		      '--text-color-tab': '#FFCB41',
		      '--bg-color': '#FFCB41',
		      '--theme-color': '#FFCB41',
		      '--bg-shadow': '#FFCB41',
		},
		// 黄色系主题
		blue: {
		      
			  '--text-color': '#2B99FF',
			  '--text-color-reverse': '#2B99FF',
			  '--text-color-tab': '#2B99FF',
			  '--bg-color': '#2B99FF',
			  '--theme-color': '#2B99FF',
			  '--bg-shadow': '#2B99FF'
		},
		// 主题方案
		scheme: ['yellow', 'blue', 'green', 'red', 'red2'],
		// 当前主题
		theme: uni.getStorageSync('theme') ? uni.getStorageSync('theme') :
		'yellow',
		
	},
	getters: {
		theme: state => state.theme,
		
		userInfo: state => state.userInfo,
		token: state => state.token,
		statusBarHeight: state => state.statusBarHeight,
		clientRect: state => state.clientRect,
		addressSelect: state => state.addressSelect,
		currentArea: state => state.currentArea,
		chat_unread:state=>state.chat_unread,
		chat_offer_current:state=>state.chat_offer_current,
		isIOS:state=>state.isIOS,
		invitation_code:state=>state.invitation_code,
	},
	mutations: {
		updateTheme(state, theme) {
		    state.theme = theme
		},
		
		setIsIOS(state,data) {
			state.isIOS = data
		},
		set_chat_info(state,data) {
			state.chat_info = data
		},
		set_chat_to(state,data) {
			state.chat_to = data
		},
		set_chat_state(state,data) {
			state.chat_state = data
		},
		set_chat_commodityInfo(state,data) {
			state.chat_commodityInfo = data
		},
		set_chat_offer_current(state,data) {
			state.chat_offer_current = data
		},
		set_chat_list(state,data){
			state.chat_list = data
			uni.setStorageSync('TZ_chat_list',data)
			if(state.chat_list && state.chat_list.length > 0){
				let num = state.chat_list.map(x=>x.unread)
				state.chat_unread =  num.reduce((prev, curr, idx, arr)=> prev + curr);
			}else{
				 state.chat_unread = 0
			}
		},
		IMINIT(state) {
			var imState = SDK.NIM.getInstance({
				...config,
				account:state.userInfo.accid,
				token:state.userInfo.im_token
			})
			Vue.prototype.$IM = imState
			state.chat_state = imState
		},
		setDefaultHouse(state, data) {
			if (state.defaultHouse && data && state.defaultHouse.zone_id == data.zone_id) return
			if(!data && !state.currentAreaSpare){
				state.defaultHouse = null
				uni.setStorageSync('TZ_defaultHouse', null)
				state.currentArea = null
				uni.setStorageSync('TZ_currentArea', null)
				return
			}else if (!data && state.currentAreaSpare) {
				state.defaultHouse = null
				uni.setStorageSync('TZ_defaultHouse', null)
				state.currentArea = {
					cityId: state.currentAreaSpare.cityId,
					cityName: state.currentAreaSpare.cityName
				}
				uni.setStorageSync('TZ_currentArea', {
					cityId: state.currentAreaSpare.cityId,
					cityName:state.currentAreaSpare.cityName
				})
				var obj = {
					id:state.currentAreaSpare.cityId,
					name: state.currentAreaSpare.cityName
				}
			} else {
				state.defaultHouse = data
				uni.setStorageSync('TZ_defaultHouse', data)
				state.currentArea = {
					cityId: data.zone_id,
					cityName: data.zone_name
				}
				uni.setStorageSync('TZ_currentArea', {
					cityId: data.zone_id,
					cityName: data.zone_name
				})
				var obj = {
					id: data.zone_id,
					name: data.zone_name
				}
				state.addressSelect = obj
				uni.setStorageSync('TZ_addressSelect', obj)
				if (state.currentAddressList && state.currentAddressList.length > 0) {
					if (state.currentAddressList.findIndex(row => row.id == obj.id) == -1) {
						state.currentAddressList = [Object.assign({}, obj), ...state.currentAddressList].slice(0,
							10)
					} else {
						state.currentAddressList.splice(state.currentAddressList.findIndex(row => row.id == obj.id),
							1)
						state.currentAddressList = [Object.assign({}, obj), ...state.currentAddressList].slice(0,
							10)
					}
				} else {
					state.currentAddressList = [Object.assign({}, obj)].slice(0, 10)
				}
				uni.setStorageSync('TZ_currentAddressList', state.currentAddressList)
			}
			
		},
		setChoiceHouse(state, data) {
			state.choiceHouse = data
		},
		setIsOpenSetting(state, data) {
			state.isOpenSetting = data
		},
		setIsOpenSettingshow(state, data) {
			state.isOpenSettingshow = data
		},
		setCityData(state, data) {
			state.cityData = data
		},
		setUser(state, data) {
			state.userInfo = data
			uni.setStorageSync('TZ_userInfo', data)
			// #ifdef APP-PLUS || MP-WEIXIN
			if(data && !state.chat_state){
				var imState = SDK.NIM.getInstance({
					...config,
					account:state.userInfo.accid,
					token:state.userInfo.im_token
				})
				console.log(99999,JSON.stringify({...config,account:state.userInfo.accid,token:state.userInfo.im_token}))
				Vue.prototype.$IM = imState
				state.chat_state = imState
				console.log('登录成功')
			}else if(!data){
				state.chat_list = []
				uni.setStorageSync('TZ_chat_list', []) 
				console.log('登出成功')
				if( state.chat_state){
					state.chat_state.disconnect()
					state.chat_state = null
					Vue.prototype.$IM = null
				}
			}
			// #endif
		},
		setCurrentArea(state, data) {
			state.currentArea = data
			uni.setStorageSync('TZ_currentArea', data)
			state.currentAreaSpare = data
			uni.setStorageSync('TZ_currentAreaSpare', data)
			if (data) {
				var obj = {
					id: data.cityId,
					name: data.cityName
				}
				state.addressSelect = obj
				uni.setStorageSync('TZ_addressSelect', obj)
				if (state.currentAddressList && state.currentAddressList.length > 0) {
					if (state.currentAddressList.findIndex(row => row.id == obj.id) == -1) {
						state.currentAddressList = [Object.assign({}, obj), ...state.currentAddressList].slice(
							0, 10)
					} else {
						state.currentAddressList.splice(state.currentAddressList.findIndex(row => row.id == obj
							.id), 1)
						state.currentAddressList = [Object.assign({}, obj), ...state.currentAddressList].slice(
							0, 10)
					}
				} else {
					state.currentAddressList = [Object.assign({}, obj)].slice(0, 10)
				}
				uni.setStorageSync('TZ_currentAddressList', state.currentAddressList)
			}
		},
		setDuanType(state, data) {
			state.duanType = data
			uni.setStorageSync('TZ_duanType', data)
		},
		setCurrentAddressList(state, data) {
			state.currentAddressList = data
			uni.setStorageSync('TZ_currentAddressList', data)
		},
		setAddressSelect(state, data) {
			state.addressSelect = data
			uni.setStorageSync('TZ_addressSelect', data)
		},
		setNewUser(state, data) {
			state.newUser = data
		},
		setToken(state, data) {
			state.token = data
			uni.setStorageSync('TZ_token', data)
		},
		setInvitationCode(state, data) {
			state.invitation_code = data
			// uni.setStorageSync('TZ_invitation_code', data)
		},
		setStatusBarHeight(state, data) {
			state.statusBarHeight = data
		},
		setClientRect(state, data) {
			state.clientRect = data
		},
		setUploadBack(state, data) {
			state.uploadBack = data
		}

	},
	actions: {

	}
})
